<?php

namespace App\System\Base\Services;

use App\System\Base\Helpers\Traits\SinglePattern;
use App\System\Base\Model\BaseRegion;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;

class RegionService extends Service
{
    use SinglePattern;

    /**
     * 得到地址列表
     */
    public function getRegionList($type, $keyword = '')
    {
        //获取不同情况下的地址列表
        switch ($type) {
            case 0:
                $region = Cache::remember('region_list_' . $type, 5, function () {
                    return BaseRegion::select('id', 'parent_id as parentId', 'name', 'pinyin')->where('deep', '2')->get();
                });
                break;
            //其他情况
            case 1:
                $region = Cache::remember('region_list_' . time(), 5, function () {
                    return BaseRegion::where('status', 1)->where('deep', '2')->select('id', 'parent_id as parentId', 'name', 'pinyin')->orderBy('pinyin', 'ASC')->get();
                });
                break;
            case 2:
                $region = BaseRegion::where('status', 1)->where('deep', '2')->where('name', 'like', '%' . $keyword . '%')->select('id', 'parent_id as parentId', 'name', 'pinyin')->get();
                break;

        }

        //转化为api输出格式
        $regionList = $region->makeHidden(['deep', 'hidden', 'created_at', 'updated_at']);
        //获取总数
        $total = $regionList->count();
        if ($type == 1) {
            $regionLists = $regionList->toArray();
            $array = array();
            foreach ($regionList as $v) {
                $arr = substr($v['pinyin'], 0, 1);
                $arr = ucfirst($arr);
                if (in_array($arr, $array)) {
                    $array[$arr][] = $v;
                } else {
                    $array[$arr][] = $v;
                }
            }
            /*  $infos =array(
                  array('char'=>'A','datas'=>array()),
                  array('char'=>'B','datas'=>array()),
                  array('char'=>'C','datas'=>array()),
                  array('char'=> 'D','datas'=>array()),
                  array('char'=>'E','datas'=>array()),
                  array('char'=>'F','datas'=>array()),
                  array('char'=> 'G','datas'=>array()),
                  array('char'=> 'H','datas'=>array()),
                  array('char'=> 'I','datas'=>array()),
                  array('char'=> 'J','datas'=>array()),
                  array('char'=>'K','datas'=>array()),
                  array('char'=>'L','datas'=>array()),
                  array('char'=> 'M','datas'=>array()),
                  array('char'=>  'N','datas'=>array()),
                  array('char'=>'O','datas'=>array()),
                  array('char'=> 'P','datas'=>array()),
                  array('char'=> 'Q','datas'=>array()),
                  array('char'=>'R','datas'=>array()),
                  array('char'=> 'S','datas'=>array()),
                  array('char'=> 'T','datas'=>array()),
                  array('char'=>  'U','datas'=>array()),
                  array('char'=> 'V','datas'=>array()),
                  array('char'=> 'W','datas'=>array()),
                  array('char'=>  'X','datas'=>array()),
                  array('char'=>'Y','datas'=>array()),
                  array('char'=> 'Z',atas'=>array())
              );

              /*foreach ($array as $key=>$val){
                  foreach ($infos as $k=>$v){
                      if($v['char'] == $key){
                          $infos[$k]=array('char'=>ucfirst($key),'datas'=>$val);
                      }
                  }
              }*/

            $infos = array();
            foreach ($array as $key => $val) {
                $infos[] = array('char' => ucfirst($key), 'datas' => $val);
            }
        }
        $data = [
            'total' => $total,
            'items' => isset($infos) ? $infos : $regionList->toArray()
        ];

        return $data;
    }
}